﻿using NearAop;
using System;
using System.Collections.Generic;
using System.Text;

namespace CS
{
[AddProxyProperty(typeof(Proxy1))]
class T04_属性外部代理
{
    public int MyProperty { get; set; }

    public void Test()
    {
        MyProperty += 1;
        MyProperty += 1;
    }
}

class Proxy1 : IAopHandlerProperty
{
    public void PropertySetHandler<T>(Lazy<AopParamInfo> aopParamInfo, object sender, T value, Func<T> getValue, Action<T> setValue)
    {
        var propName = aopParamInfo.GetMemberName();
        var msg = $"{sender}.{propName}被修改, 修改前:{getValue()}, 新值:{value}";
        Console.WriteLine(msg);
        setValue(value);//执行修改
    }

    public T PropertyGetHandler<T>(Lazy<AopParamInfo> aopParamInfo, object sender, Func<T> getValue)
    {
        var propName = aopParamInfo.GetMemberName();
        var result = getValue();
        var msg = $"{sender}.{propName}被读取,值:{result}";
        Console.WriteLine(msg);
        return result;
    }
}
}
